using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ObviousCode.Stately;
using System.IO;
using ObviousCode.Stately.Exceptions;

namespace ObviousCode.Stately.Compiler.ContextMenuHandler.Values
{
	public partial class ValuesReader
	{
        static string CLASSNAME_LABEL = "Classname:";
        static string NAMESPACE_LABEL = "Namespace:";
        static string DATA_CONTEXT_LABEL = "Data Context:";

		public void Start(ValuesContext context, Action<ValuesContext> callback)
		{
            ValuesContext valuesContext = context;
            
			valuesContext.Callback = callback;

			_stately.Start(Step.LoadFile, valuesContext);
		}

        public event EventHandler MapFilenameNotSet;
        private ITransitionRequest HandleNotifyFilenameNotPassed(ValuesContext valuesContext)
        {
            if (MapFilenameNotSet != null) MapFilenameNotSet(this, EventArgs.Empty);
            else
            {
                throw new Stately.Exceptions.StatelyProcessException(
                    new InvalidOperationException("Map Filename Should Not Be Null")
                    );
            }
            return HaltRequest.Instance;
        }

		private ITransitionRequest HandleLoadFile(ValuesContext valuesContext)
		{
            if (string.IsNullOrWhiteSpace(valuesContext.MapFilename))
            {
                return _stately.T(Step.NotifyFilenameNotPassed, valuesContext);
            }

            if (File.Exists(valuesContext.MapFilename))
            {
                valuesContext.Stream = File.OpenRead(valuesContext.MapFilename);

                return _stately.T(Step.SetValues, valuesContext);
            }
            else
            {
                return _stately.T(Step.NotifyFileNotFound, valuesContext);
            }
		}

        public event EventHandler FileNotFound;
		private ITransitionRequest HandleNotifyFileNotFound(ValuesContext valuesContext)
		{
            if (FileNotFound != null)
            {
                FileNotFound(this, EventArgs.Empty);
            }
            else
            {
                throw new StatelyProcessException(
                    new FileNotFoundException(valuesContext.MapFilename));
            }
            return HaltRequest.Instance;
		}

		private ITransitionRequest HandleSetValues(ValuesContext valuesContext)
		{
            using (StreamReader reader = new StreamReader(valuesContext.Stream))
            {
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine().Replace("#", "").Trim();

                    if (line.StartsWith(CLASSNAME_LABEL)) valuesContext.Classname = line.Substring(CLASSNAME_LABEL.Length).Trim();
                    if (line.StartsWith(NAMESPACE_LABEL)) valuesContext.Namespace = line.Substring(NAMESPACE_LABEL.Length).Trim();
                    if (line.StartsWith(DATA_CONTEXT_LABEL)) valuesContext.DataContext = line.Substring(DATA_CONTEXT_LABEL.Length).Trim();
                }                
            }

            valuesContext.Stream.Dispose();

			return _stately.T(Step.NotifyValuesFound, valuesContext);
		}

        public event EventHandler ValueLocationComplete;
		private ITransitionRequest HandleNotifyValuesFound(ValuesContext valuesContext)
		{
            if (ValueLocationComplete != null) ValueLocationComplete(this, EventArgs.Empty);

            if (valuesContext.Callback != null) valuesContext.Callback(valuesContext);

            return HaltRequest.Instance;
		}


	}
}

